package com.jk.utile;

import com.jk.controller.LogRegController;
import com.jk.mapper.LogMapper;
import com.jk.service.LogService;
import com.jk.vo.UserInfo;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import com.rabbitmq.client.Channel;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Map;

/**
 * 监听处理登陆注册消息
 *
 * @author 李卓
 * @create 2017-12-24 16:09
 **/
/*@Configuration*/
public class ListenerAmqp {

    public static final String EXCHANGE   = "spring-boot-exchange";
    public static final String ROUTINGKEY = "spring-boot-routingKey";
    @Autowired
    private LogMapper logMapper;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses("192.168.1.164:5672");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true); //设置 消息回调 如果业务不需要发送消息确认机制，这句话可直接设置为false
        return connectionFactory;
    }


    @Bean
    public DirectExchange defaultExchange() {
        return new DirectExchange(EXCHANGE);
    }

    @Bean
    public Queue queue() {
        return new Queue("spring-boot-log-queue", true); //设置队列持久 化
    }


    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(defaultExchange()).with(ListenerAmqp.ROUTINGKEY);
    }

    @Bean
    public SimpleMessageListenerContainer messageContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
        container.setQueues(queue());
        container.setExposeListenerChannel(true);
        container.setMaxConcurrentConsumers(1);
        container.setConcurrentConsumers(1);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL); //设置确认模式手工确认
        container.setMessageListener(new ChannelAwareMessageListener() {


            public void onMessage(Message message, Channel channel) throws Exception {

                byte[] byt= message.getBody();
                Object obj = null;
                try {
                    ByteArrayInputStream bi = new ByteArrayInputStream(byt);
                    ObjectInputStream oi = new ObjectInputStream(bi);
                    obj = oi.readObject();
                    bi.close();
                    oi.close();
                } catch (Exception e) {
                    System.out.println("translation" + e.getMessage());
                    e.printStackTrace();
                }
                 int i = logMapper.insertLog((UserInfo)obj);
             if(i==1){
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); //确认消息成功消费
            }
            }
        });
        return container;
    }
}
